﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using Strawberry.Filters;
using Strawberry.Models;
using Service;
using Microsoft.Practices.Unity;
using Strawberry.Provider;

namespace Strawberry.Controllers
{

    public partial class AccountController : Controller
    {

        [Dependency]
        public IUserManager UserManager { get; set; }


        public virtual ActionResult Login()
        {

            return View();
        }

        public virtual ActionResult Logoff()
        {
            Session.Clear();
            return RedirectToAction(MVC.Home.Index());
        }


        [HttpPost]
        public virtual ActionResult Login(LoginModel model)
        {

            var user = UserManager.Connexion(model.Email, model.Password);
            if (user == null)
            {
                ModelState.AddModelError("Erreur Connexion", "Utilisateur non connu");
                return View();

            }
            else
            {
                
                Session[SessionVariable.IDUSER] = user.Id;
                Session[SessionVariable.ROLE] = user.Role;
                Session[SessionVariable.USERNAME] = user.Name;
                return RedirectToAction(MVC.Home.Index());

            }


            

        }

    }
}
